package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.RightsMapper;
import com.jt.pojo.Rights;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RightsServiceImpl implements RightsService{

    @Autowired
    public RightsMapper rightsMapper;

    /**
     * 1. 查询所有的1级信息 parent_id = 0
     * 2. 查询所有的1级下的2级信息 parent_id = 1
     * @return
     */
    @Override
    public List<Rights> getRighrsList() {
        //1. 查询1级权限信息
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent_id",0);
        List<Rights> oneList = rightsMapper.selectList(queryWrapper);
        //2. 查询1级下的2级权限list集合
        for (Rights rights : oneList){
            queryWrapper.clear();  //将where条件清空之后重新添加
            queryWrapper.eq("parent_id",rights.getId());
            List<Rights> children = rightsMapper.selectList(queryWrapper);
            //查询子级之后,将数据赋值给父级 实现嵌套结构
            rights.setChildren(children);
        }
        return oneList;

    }
}
